package com.zwd.deep.common;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 返回前端的数据包装类
 *
 * @author wanjunhong
 */
@Data
@Accessors(chain = true)
public class RequestResult<T> implements Serializable {

    /**
     * 状态码
     *
     */
    private int code;

    /**
     * 返回的信息
     *
     */
    private String message;

    /**
     * 返回的数据
     */
    private T data;

    /**
     * 响应花费的时间
     *
     */
    private long spendTime;

    /**
     * 成功无数据返回
     *
     * @param msg: 消息
     * @return RequestResult<Void> 封装的返回结果
     * @author wanjunhong
     */
    public static RequestResult<Void> success(ResultEnum msg) {
        RequestResult<Void> requestResult = new RequestResult<>();
        requestResult.setCode(ResultEnum.SUCCESS.getCode());
        requestResult.setMessage(msg.getMsg());
        return requestResult;
    }

    /**
     * 成功有数据返回
     *
     * @param data: 数据
     * @param msg:  消息
     * @return RequestResult<T> 封装的返回结果
     * @author wanjunhong
     */
    public static <T> RequestResult<T> success(T data, ResultEnum msg) {
        RequestResult<T> requestResult = new RequestResult<>();
        requestResult.setCode(ResultEnum.SUCCESS.getCode());
        requestResult.setMessage(msg.getMsg());
        requestResult.setData(data);
        return requestResult;
    }

    /**
     * 成功有数据返回,无消息返回
     *
     * @param data: 数据
     * @return RequestResult<T> 封装的返回结果
     * @author wanjunhong
     */
    public static <T> RequestResult<T> success(T data) {
        RequestResult<T> requestResult = new RequestResult<>();
        requestResult.setCode(ResultEnum.SUCCESS.getCode());
        requestResult.setMessage(ResultEnum.SUCCESS.getMsg());
        requestResult.setData(data);
        return requestResult;
    }

    /**
     * 失败无数据返回
     *
     * @param msg: 消息
     * @return RequestResult<Void>
     * @author wanjunhong
     */
    public static RequestResult<Void> fail(ResultEnum msg) {
        RequestResult<Void> requestResult = new RequestResult<>();
        requestResult.setCode(msg.getCode());
        requestResult.setMessage(msg.getMsg());
        return requestResult;
    }

    /**
     * 失败无消息返回，有数据
     *
     * @param data: 数据
     * @return RequestResult<Void>
     * @author wanjunhong
     */
    public static <T> RequestResult<T> fail(T data) {
        RequestResult<T> requestResult = new RequestResult<>();
        requestResult.setCode(ResultEnum.FAIL.getCode());
        requestResult.setMessage(ResultEnum.FAIL.getMsg());
        requestResult.setData(data);
        return requestResult;
    }

    /**
     * 异常信息返回
     *
     * @param msg: 消息
     * @return RequestResult<Void>
     * @author wanjunhong
     */
    public static RequestResult<Void> fail(String msg) {
        RequestResult<Void> requestResult = new RequestResult<>();
        requestResult.setCode(ResultEnum.FAIL.getCode());
        requestResult.setMessage(msg);
        return requestResult;
    }

    /**
     * 失败有数据返回
     *
     * @param data: 数据
     * @param msg:  消息
     * @return RequestResult<T> 封装的返回结果
     * @author wanjunhong
     */
    public static <T> RequestResult<T> fail(T data, ResultEnum msg) {
        RequestResult<T> requestResult = new RequestResult<>();
        requestResult.setCode(msg.getCode());
        requestResult.setMessage(msg.getMsg());
        requestResult.setData(data);
        return requestResult;
    }

    /**
     * 返回的数据是boolean类型
     *
     * @return RequestResult<T> 封装的返回结果
     * @author wanjunhong
     */
    public static RequestResult<Boolean> bool(boolean data) {
        RequestResult<Boolean> requestResult = new RequestResult<>();
        requestResult.setCode(data ? ResultEnum.SUCCESS.getCode() : ResultEnum.FAIL.getCode());
        requestResult.setMessage(data ? ResultEnum.SUCCESS.getMsg() : ResultEnum.FAIL.getMsg());
        requestResult.setData(data);
        return requestResult;
    }
}
